package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "sku管理")
@RestController
@RequestMapping("/admin/product")
public class SkuManageController {

    @Autowired
    private ManageService manageService;

    /**
     * 保存skuInfo 数据
     * @param skuInfo
     * @return
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        //  调用服务层方法.
        this.manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    /**
     * 根据三级分类Id 获取数据
     * @param page
     * @param limit
     * @return
     */
    //  http://localhost/admin/product/list/1/10?category3Id=61
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuInfoList(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 Long category3Id){
        Page<SkuInfo> skuInfoPage = new Page<>(page,limit);
        //  调用服务层方法.
        IPage<SkuInfo> iPage = this.manageService.getSkuInfoList(skuInfoPage,category3Id);
        return Result.ok(iPage);
    }

    /**
     * 根据skuId 进行上架
     * @param skuId
     * @return
     */
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        //  调用服务层方法。
        this.manageService.onSale(skuId);
        return Result.ok();
    }

    /**
     * 根据skuId 进行下架
     * @param skuId
     * @return
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        //  调用服务层方法。
        this.manageService.cancelSale(skuId);
        return Result.ok();
    }
}
